package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class CheckBoxesSlice extends AbilitySlice {

    private Image back_button, f_img1, f_img2, f_img3, d_img1, d_img2, y_img, a_img, r_img, q_img, q_img1, q_img2, x_img;
    private Text text_title;
    private Component layout1, layout2, layout3, d_layout1, d_layout2, y_layout, a_layout, r_layout, q_layout, q_layout1, q_layout2;
    private boolean layout1_ = false, layout2_ = false, layout3_ = false, d1_layout = false, d2_layout = false, layout_y = false, layout_a = false, layout_r = false, layout_q = false, layout_q1 = false, layout_q2 = false,layout_x = false;
    private Button t_btn, f_btn;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_check_boxes);
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0){
            if (MainAbilitySlice.all.containsKey("CheckBoxes & RadioButton")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("CheckBoxes & RadioButton")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("CheckBoxes & RadioButton", integer);
                        pf.flushSync();
                    }
                }
            }else {
                pf.putInt("CheckBoxes & RadioButton", 1);
                pf.flushSync();
            }
        }else {
            pf.putInt("CheckBoxes & RadioButton", 1);
            pf.flushSync();
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("CheckBoxes & RadioButton");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        layout1 = findComponentById(ResourceTable.Id_layout1);
        layout2 = findComponentById(ResourceTable.Id_layout2);
        layout3 = findComponentById(ResourceTable.Id_layout3);
        d_layout1 = findComponentById(ResourceTable.Id_d_layout1);
        d_layout2 = findComponentById(ResourceTable.Id_d_layout2);
        y_layout = findComponentById(ResourceTable.Id_y_layout);
        a_layout = findComponentById(ResourceTable.Id_a_layout);
        r_layout = findComponentById(ResourceTable.Id_r_layout);
        q_layout = findComponentById(ResourceTable.Id_q_layout);
        q_layout1 = findComponentById(ResourceTable.Id_q_layout1);
        q_layout2 = findComponentById(ResourceTable.Id_q_layout2);
        f_img1 = (Image) findComponentById(ResourceTable.Id_f_img1);
        f_img2 = (Image) findComponentById(ResourceTable.Id_f_img2);
        f_img3 = (Image) findComponentById(ResourceTable.Id_f_img3);
        d_img1 = (Image) findComponentById(ResourceTable.Id_d_img1);
        d_img2 = (Image) findComponentById(ResourceTable.Id_d_img2);
        q_img = (Image) findComponentById(ResourceTable.Id_q_img);
        q_img1 = (Image) findComponentById(ResourceTable.Id_q_img1);
        q_img2 = (Image) findComponentById(ResourceTable.Id_q_img2);
        y_img = (Image) findComponentById(ResourceTable.Id_y_img);
        a_img = (Image) findComponentById(ResourceTable.Id_a_img);
        r_img = (Image) findComponentById(ResourceTable.Id_r_img);
        r_img = (Image) findComponentById(ResourceTable.Id_r_img);
        x_img = (Image) findComponentById(ResourceTable.Id_x_img);
        t_btn = (Button) findComponentById(ResourceTable.Id_t_btn);
        f_btn = (Button) findComponentById(ResourceTable.Id_f_btn);
        a_img.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_black_24dp));
        layout1.setClickedListener(component -> {
            if (f_img1 != null && layout1_ == false) {
                f_img1.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                layout1_ = true;
            } else {
                f_img1.setPixelMap(ResourceTable.Media_checkbox_false1);
                layout1_ = false;
            }
        });
        layout2.setClickedListener(component -> {
            if (f_img2 != null && layout2_ == false) {
                f_img2.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                layout2_ = true;
            } else {
                f_img2.setPixelMap(ResourceTable.Media_checkbox_false1);
                layout2_ = false;
            }
        });
        layout3.setClickedListener(component -> {
            if (f_img3 != null && layout3_ == false) {
                f_img3.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                layout3_ = true;
            } else {
                f_img3.setPixelMap(ResourceTable.Media_checkbox_false1);
                layout3_ = false;
            }
        });
        d_layout1.setClickedListener(component -> {
            if (!d1_layout) {
                d_img1.setPixelMap(ResourceTable.Media_check_round_true);
                d1_layout = true;
                if (d2_layout) {
                    d_img2.setPixelMap(ResourceTable.Media_check_round_false);
                    d2_layout = false;
                }
            }
        });
        d_layout2.setClickedListener(component -> {
            if (!d2_layout) {
                d_img2.setPixelMap(ResourceTable.Media_check_round_true);
                d2_layout = true;
                if (d1_layout) {
                    d_img1.setPixelMap(ResourceTable.Media_check_round_false);
                    d1_layout = false;
                }
            }
        });

        y_layout.setClickedListener(component -> {
            if (!layout_y) {
                y_img.setPixelMap(ResourceTable.Media_mail_selected);
                layout_y = true;
                if (layout_a || layout_r) {
                    a_img.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_black_24dp));
                    r_img.setPixelMap(ResourceTable.Media_profile_normal);
                    layout_a = false;
                    layout_r = false;
                }
            }
        });
        a_layout.setClickedListener(component -> {
            if (!layout_a) {
                a_img.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_yellow_24dp));
                layout_a = true;
                if (layout_y || layout_r) {
                    y_img.setPixelMap(ResourceTable.Media_mail_normal);
                    r_img.setPixelMap(ResourceTable.Media_profile_normal);
                    layout_y = false;
                    layout_r = false;
                }
            }
        });
        r_layout.setClickedListener(component -> {
            if (!layout_r) {
                r_img.setPixelMap(ResourceTable.Media_profile_selected);
                layout_r = true;
                if (layout_y || layout_a) {
                    y_img.setPixelMap(ResourceTable.Media_mail_normal);
                    a_img.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_black_24dp));
                    layout_y = false;
                    layout_a = false;
                }
            }
        });

        q_layout.setClickedListener(component -> {
            if (!layout_q) {
                q_img.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                layout_q = true;
                if (!layout_q1 || !layout_q2) {
                    q_img1.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                    q_img2.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                    layout_q1 = true;
                    layout_q2 = true;
                }
            } else {
                q_img.setPixelMap(ResourceTable.Media_checkbox_false1);
                layout_q = false;
                if (layout_q1 && layout_q2) {
                    q_img1.setPixelMap(ResourceTable.Media_checkbox_false1);
                    q_img2.setPixelMap(ResourceTable.Media_checkbox_false1);
                    layout_q1 = false;
                    layout_q2 = false;
                }
            }
        });
        q_layout1.setClickedListener(component -> {
            if (!layout_q1) {
                q_img1.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                layout_q1 = true;
                if (!layout_q2) {
                    q_img.setPixelMap(ResourceTable.Media_check_line);
                } else {
                    q_img.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                }
            } else {
                q_img1.setPixelMap(ResourceTable.Media_checkbox_false1);
                layout_q1 = false;
                if (!layout_q2) {
                    q_img.setPixelMap(ResourceTable.Media_checkbox_false1);
                } else {
                    q_img.setPixelMap(ResourceTable.Media_check_line);
                }
            }
        });
        q_layout2.setClickedListener(component -> {
            if (!layout_q2) {
                q_img2.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                layout_q2 = true;
                if (!layout_q1) {
                    q_img.setPixelMap(ResourceTable.Media_check_line);
                } else {
                    q_img.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                }
            } else {
                q_img2.setPixelMap(ResourceTable.Media_checkbox_false1);
                layout_q2 = false;
                if (!layout_q1) {
                    q_img.setPixelMap(ResourceTable.Media_checkbox_false1);
                } else {
                    q_img.setPixelMap(ResourceTable.Media_check_line);
                }
            }
        });
        x_img.setClickedListener(component -> {
            if (!layout_x){
                x_img.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
                layout_x=true;
            }else {
                x_img.setPixelMap(ResourceTable.Media_checkbox_false1);
                layout_x=false;
            }
        });
        t_btn.setClickedListener(component -> {
            x_img.setPixelMap(ResourceTable.Media_checkbox_yellow_true);
            layout_x=true;
        });
        f_btn.setClickedListener(component -> {
            x_img.setPixelMap(ResourceTable.Media_checkbox_false1);
            layout_x=false;
        });
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
